(ns main
  (load-file "src/engine/rete.clj")
  (:require [clojure.xml :as xml])

  (:import (javax.swing JFrame JTextField JButton JPanel JLabel JTextArea JScrollPane BoxLayout BorderFactory)
           (java.awt.event ActionListener)
           (java.awt GridLayout BorderLayout FlowLayout Dimension Color ))
  )

;MAIN
(def xml-rules (xml/parse "rules/rules.xml"))
(def xml-facts (xml/parse "facts/facts2.xml"))

(rete/createNet xml-rules)
(rete/runEngine xml-facts)


;FUNCIONES DE LA GUI
;(defn addAction
;  "Creates a new button and adds the works too it."
;  [button textField label shareName]
;  (. button addActionListener
;      (proxy [ActionListener] []
;        (actionPerformed [e]
;          (dosync
;            (if (re-find #"^[^=]*=[^=]*$" (. textField getText))
;              (let [nodo (core.Node.)
;                    rule (. (. textField getText) split "=")]      
;                
;                (rete/loadNode nodo "input" (first rule) (second rule))            
;                (rete/addFact nodo)
;                
;                (. textField setText "")            
;                (def texto "")
;                (println "Working Memory : " (. @rete/workingMemory toString))
;                ;(doseq [x (. (. @rete/workingMemory toString) split "#")]
;                ;(println "X" x)
;                ;(def texto (str texto "\n" x))
;                ;(println "indexof" (.indexOf x "<Node rule"))                
;                ;(if (zero? (.indexOf x "<Node rule"))
;                 ; (. label setText (. x replaceAll "<Node " "<Result= ")))
;                 ;(. label setText texto))
           ;)
;          )          
;          )
;        )
;        )
;      )
  ;)
;  )
  


;GUI
;(let [frame (JFrame. "Inference Engine")
;        okButton (JButton. "OK")
;        cleanButton (JButton. "Delete")
;        ;imprimirButton (JButton. "View Net")
;        text-field-accion (JTextField. "Insert share's name" 30)
;        text-field (JTextField. "" 30)
;        outter-pane (JPanel. (BorderLayout. 0 1))
;        southPane (JPanel. (FlowLayout.))
;        northPane (JPanel. (BorderLayout. 1 0))
;        centerPane (JScrollPane.)
;        labelAccion (JLabel. "Share: ")
;        labelRegla (JLabel. "New Fact: ")
;        areaWK (JTextArea. "RESULT:")]  
;  (addAction okButton text-field areaWK text-field-accion) ;Ok button

;  (. cleanButton addActionListener ; Clean button
;      (proxy [ActionListener] []
;        (actionPerformed [e]
;          (dosync 
;            (ref-set rete/reteNet (hash-map))
;            (ref-set rete/workingMemory #{})
;            )
;          (rete/createNet xml-rules)
;          (. areaWK setText "")
;        )))
  
  ;(. imprimirButton addActionListener ;View Net button
  ;    (proxy [ActionListener] []
  ;      (actionPerformed [e]
  ;        ;(println "\nReteNet")
  ;        (println @rete/reteNet)
  ;      )))
;  (doto areaWK
;    (.setEditable false)
;    )
;  (doto labelAccion
;    (.setSize 30 40)
;    )
;  (doto labelRegla
;    (.setSize 30 40)
;    )
;  (doto southPane
;    (.add cleanButton)
;    ;(.add imprimirButton)
;    (.add okButton)
;    (.setBorder (. BorderFactory createLineBorder (Color/black)))      
;    )
;  (doto northPane 
;    (.add labelAccion BorderLayout/CENTER)
;    (.add text-field-accion BorderLayout/EAST)
;    (.add labelRegla BorderLayout/CENTER)
;    (.add text-field BorderLayout/EAST)
;    (.setBorder (. BorderFactory createLineBorder (Color/black)))
;    (.setLayout (BoxLayout. northPane BoxLayout/Y_AXIS))
;    )
;  (doto centerPane      
;    (.setViewportView areaWK)    
;    (.setBorder (. BorderFactory createLineBorder (Color/black)))      
;    )
;  (doto outter-pane
;    (.add northPane BorderLayout/NORTH)
;    (.add centerPane BorderLayout/CENTER)
;    (.add southPane BorderLayout/SOUTH)
;    )
;  (doto frame
;    (.add outter-pane)
;    (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
;    (.setResizable false)
;    (.setSize 300 300)
;    (.setVisible true)
;    )
;  )
